package com.gin.no_gay_alliance.param;

import android.content.Context;
import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;

import com.gin.nga.params.post.PrepareParam;
import com.gin.no_gay_alliance.activity.PostActivity;

/**
 * 准备发帖参数
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/16 14:37
 **/
public class PreparePostParam implements Parcelable {

    public static final Creator<PreparePostParam> CREATOR = new Creator<PreparePostParam>() {
        @Override
        public PreparePostParam createFromParcel(Parcel in) {
            return new PreparePostParam(in);
        }

        @Override
        public PreparePostParam[] newArray(int size) {
            return new PreparePostParam[size];
        }
    };
    /**
     * 发帖动作
     */
    public Action action;
    /**
     * 版面id
     */
    public Long forumId;
    /**
     * 主题id
     */
    public Long topicId;
    /**
     * 回复id
     */
    public Long replyId;
    /**
     * 合集id
     */
    public Long colTid;
    /**
     * 是否为评论
     */
    public boolean isComment;

    public PreparePostParam() {
    }

    protected PreparePostParam(Parcel in) {
        action = in.readParcelable(Action.class.getClassLoader());
        if (in.readByte() == 0) {
            forumId = null;
        } else {
            forumId = in.readLong();
        }
        if (in.readByte() == 0) {
            topicId = null;
        } else {
            topicId = in.readLong();
        }
        if (in.readByte() == 0) {
            replyId = null;
        } else {
            replyId = in.readLong();
        }
        if (in.readByte() == 0) {
            colTid = null;
        } else {
            colTid = in.readLong();
        }
        isComment = in.readByte() != 0;
    }

    /**
     * 发布新主题
     *
     * @param forumId 版面
     * @param colTid  合集
     */
    public PreparePostParam(Long forumId, Long colTid) {
        this.action = Action.NEW;
        this.forumId = forumId;
        this.colTid = colTid;
    }

    /**
     * 编辑、回复、引用
     *
     * @param action  操作
     * @param topicId 主题
     * @param replyId 回复
     */
    public PreparePostParam(Action action, Long topicId, Long replyId) {
        this.action = action;
        this.topicId = topicId;
        this.replyId = replyId;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(action, flags);
        if (forumId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(forumId);
        }
        if (topicId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(topicId);
        }
        if (replyId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(replyId);
        }
        if (colTid == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(colTid);
        }
        dest.writeByte((byte) (isComment ? 1 : 0));
    }

    public Intent buildIntent(Context context) {
        Intent intent = new Intent(context, PostActivity.class);
        intent.putExtra(PreparePostParam.class.getName(), this);
        return intent;
    }

    public PrepareParam buildParam() {
        switch (action) {
            case NEW:
                return PrepareParam.newTopicParam(forumId, colTid);
            case QUOTE:
                return PrepareParam.quoteParam(topicId, replyId);
            case MODIFY:
                return PrepareParam.modifyParam(topicId, replyId);
            case REPLY:
                return PrepareParam.replyParam(topicId, replyId, isComment);
            default:
                throw new RuntimeException("非法的类型");
        }
    }

    public enum Action implements Parcelable {
        /**
         * 新主题
         */
        NEW,
        /**
         * 回复或评论(贴条)
         */
        REPLY,
        /**
         * 引用
         */
        QUOTE,
        /**
         * 编辑
         */
        MODIFY,
        ;

        public static final Creator<Action> CREATOR = new Creator<Action>() {
            @Override
            public Action createFromParcel(Parcel in) {
                return Action.values()[in.readInt()];
            }

            @Override
            public Action[] newArray(int size) {
                return new Action[size];
            }
        };

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(this.ordinal());
        }
    }
}
